import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:yw_bapp/model/applay_withdrawal_model.dart';
import 'package:yw_bapp/model/bind_bank_model.dart';
import 'package:yw_bapp/model/block_money_model.dart';
import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:yw_bapp/model/news_list_model.dart';
import 'package:yw_bapp/utils/loacl_storage.dart';
import 'dart:io';
import 'dart:convert';

const BASER_URL = "https://ywapidev.lemobj.com/api/";
var headerMap = new Map<String, String>.from(
    {"content-type": "application/json", "X-YOUWEI-PLAT": 'wx_b_user'});
// 申请提现接口地址
const applayURL = BASER_URL + "manager/withdraw/post_apply_withdraw";

// 获取提现消息列表接口地址
const newsURL =
    BASER_URL + "manager/withdraw/get_storeapply_cashout_list?page=";
// 获取提现消息列表接口地址
const bindBankURL =
    BASER_URL + "manager/withdraw/store_bind_card";

// 提现相关接口
class WithdrawalDao {
// 申请提现
  static Future<ApplayWithdrawalModel> ApplayWithdrawal(
      dynamic scorenum,
      dynamic rmbPrice,
      dynamic payPrice,
      dynamic servicePrice,
      dynamic scoreRatio,
      dynamic withdrawRatio) async {
    var params = {
      "scorenum": scorenum,
      "rmb_price": rmbPrice,
      "pay_price": payPrice,
      "service_price": servicePrice,
      "score_ratio": scoreRatio,
      "withdraw_ratio": withdrawRatio
    };
    var storeID = await getStorage('Store-ID');
    var token = await getStorage('Token');
    // headerMap['Token'] = token.toString();
    // headerMap['Store-ID'] = storeID.toString();
    headerMap['Token'] = '529';
    headerMap['Store-ID'] = '226';
    final response = await http.post(applayURL,
        headers: headerMap, body: jsonEncode(params));
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      print(result);
      return ApplayWithdrawalModel.fromJson(result);
    } else {
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }

  // 获取提现消息列表
  static Future<NewsListModel> GetNewsList(int pageIndex,
      {String start_time = '', String end_time = ''}) async {
    var storeID = await getStorage('Store-ID');
    var token = await getStorage('Token');
    // headerMap['Token'] = token.toString();
    // headerMap['Store-ID'] = storeID.toString();
    headerMap['Token'] = '529';
    headerMap['Store-ID'] = '226';
    final response = await http.get(
        newsURL +
            '$pageIndex' +
            '&start_time=' +
            '$start_time ' +
            '&end_time=' +
            '$end_time',
        headers: headerMap);
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return NewsListModel.fromJson(result);
    } else {
      ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
        return Center(
          child: Text('网络错误啦'),
        );
      };
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }

  
  // 绑定银行卡
  static Future<BindBankModel> BindBankAccount(String accountName,String bankName,String subBankName,String bankAccount) async {
    var storeID = await getStorage('Store-ID');
    var token = await getStorage('Token');
    // headerMap['Token'] = token.toString();
    // headerMap['Store-ID'] = storeID.toString();
    headerMap['Token'] = '529';
    headerMap['Store-ID'] = '226';
    var params = {
        	"account_name": accountName,
          "bank_name": bankName,
          "sub_bank_name": subBankName,
          "bank_account": bankAccount
    };
    final response = await http.post(bindBankURL,
        headers: headerMap, body: jsonEncode(params));
    if (response.statusCode == 200) {
      // 解决中文乱码
      Utf8Decoder utf8decoder = Utf8Decoder();
      var result = json.decode(utf8decoder.convert(response.bodyBytes));
      return BindBankModel.fromJson(result);
    } else {
      ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
        return Center(
          child: Text('网络错误啦'),
        );
      };
      print('请求接口失败');
      throw Exception('请求接口失败');
    }
  }
}
